package HelperFinancial.User;

import HelperFinancial.Class.Debito;
import HelperFinancial.DataBase.DebitoAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class GastosViewActivity extends Activity {
	private GastosViewActivity frame = this;
	private TextView date;
//	private TextView notes;
	private TextView name;
	private TextView value;
	private TextView portions;
	private TextView parcelaCorrente;
	private TextView type;

	int idPai;
	int corrente;
	
	private Debito gasto;

	private DebitoAdapter adapter;

	/** Called when the activity is first create */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gasto_view);

		adapter = DebitoAdapter.getInstance(frame);

		gasto = adapter.getCountById(getIntent().getIntExtra("id", 0));
		
		date = (TextView) findViewById(R.id.date_pay_gasto_view);
		date.setText(gasto.getDate());
//		notes = (TextView) findViewById(R.id.notes_gasto_view);
//		notes.setText(gasto.getNotes());
		name = (TextView) findViewById(R.id.name_gasto_view);
		name.setText(gasto.getName());
		portions = (TextView) findViewById(R.id.portions_gasto_view);
		portions.setText(gasto.getPortions() + "");
		parcelaCorrente = (TextView) findViewById(R.id.corrente_gasto_view);
		parcelaCorrente.setText(gasto.getParcelaCorrente() + "");
		value = (TextView) findViewById(R.id.value_gasto_view);
		value.setText(gasto.getValue() + "");
		type = (TextView) findViewById(R.id.category_gasto_view);
		type.setText(gasto.getType());
		
		Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        int width = display.getWidth();
		
		Button delete = (Button) findViewById(R.id.delete_gasto_view);
		delete.setOnClickListener(onDelete);
		delete.setWidth(width/2);
		Button update = (Button) findViewById(R.id.update_gasto_view);
		update.setOnClickListener(onUpdate);
		update.setWidth(width/2);
	}

	private View.OnClickListener onDelete = new View.OnClickListener() {
		public void onClick(View arg0) {

			AlertDialog.Builder builder = new AlertDialog.Builder(frame);
			builder.setMessage("Deseja realmente excluir?")
					.setCancelable(false)
					.setPositiveButton("Yes",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									if (gasto.getPortions() == 1 || gasto.getParcelaCorrente() == gasto.getPortions()) 
										adapter.removeCount(gasto.getId());
									else
										adapter.removeParcelada(gasto.getParcelaCorrente(), gasto.getIdPai());
									finish();
									//startActivity(new Intent(frame, ExtractActivity.class));
								}
							})
					.setNegativeButton("No",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							});
			AlertDialog alert = builder.create();
			alert.show();

		}
	};

	private View.OnClickListener onUpdate = new View.OnClickListener() {
		public void onClick(View arg0) {
			Intent intent = new Intent(frame, DebitoUpdateActivity.class);
			Integer id = gasto.getId();
			if (id != null && id > 0) {
				intent.putExtra("id", gasto.getId());
				finish();
				startActivity(intent);
			}

		}

	};

	@Override
	public void onDestroy() {
		super.onDestroy();
	}
}